import http from '../utils/request';
import { useAuthStore } from '../store/auth';

// AI模型API前缀
const AI_MODEL_PREFIX = '[AIModel]';

// AI模型类型枚举
export enum AIModelType {
  OPENAI = 'openai',
  AZURE = 'azure',
  ANTHROPIC = 'anthropic',
  BAIDU = 'baidu',
  XFYUN = 'xfyun',
  ZHIPU = 'zhipu',
  SILICON_FLOW = 'silicon_flow', // 添加硅基流动类型
  OTHER = 'other',
}

// AI模型配置类型
export interface AIModelConfig {
  id?: number | string;
  name: string;
  type: AIModelType;
  apiKey: string;
  apiSecret?: string;
  orgId?: string;
  baseUrl: string;
  model?: string;
  maxTokens?: number;
  temperature?: number;
  topP?: number;
  timeout?: number;
  isDefault: boolean;
  isEnabled: boolean;
  extraConfig?: Record<string, any>;
  description?: string;
  usageCount?: number;
  totalTokens?: number;
  createdAt?: Date;
  updatedAt?: Date;
}

/**
 * AI模型API
 */
export const aiModelApi = {
  /**
   * 获取AI模型配置列表
   * @returns {Promise} - HTTP GET请求的Promise对象
   */
  getList: () => {
    // 从auth store获取当前token
    const authStore = useAuthStore();
    const token = authStore.getCurrentToken;
    console.log(`${AI_MODEL_PREFIX} API 获取配置列表，当前token:`, token);
    
    return http.get('/ai-model-config', {}, false, true);
  },
  
  /**
   * 获取AI模型配置详情
   * @param {number|string} id - 配置ID
   * @returns {Promise} - HTTP GET请求的Promise对象
   */
  getDetail: (id: number | string) => {
    console.log(`${AI_MODEL_PREFIX} API 获取配置详情:`, id);
    return http.get(`/ai-model-config/${id}`, {}, false, true);
  },
  
  /**
   * 获取默认AI模型配置
   * @returns {Promise} - HTTP GET请求的Promise对象
   */
  getDefault: () => {
    console.log(`${AI_MODEL_PREFIX} API 获取默认配置`);
    return http.get('/ai-model-config/default', {}, false, true);
  },
  
  /**
   * 创建AI模型配置
   * @param {AIModelConfig} data - 配置信息
   * @returns {Promise} - HTTP POST请求的Promise对象
   */
  create: (data: AIModelConfig) => {
    console.log(`${AI_MODEL_PREFIX} API 创建配置`);
    return http.post('/ai-model-config', data, false, true, true);
  },
  
  /**
   * 更新AI模型配置
   * @param {number|string} id - 配置ID
   * @param {AIModelConfig} data - 配置信息
   * @returns {Promise} - HTTP PUT请求的Promise对象
   */
  update: (id: number | string, data: AIModelConfig) => {
    console.log(`${AI_MODEL_PREFIX} API 发送更新请求: ID=${id}, URL=/api/ai-model-config/${id}`);
    // 创建一个不包含id字段的新对象，避免后端处理问题
    const { id: _, ...updateData } = data;
    return http.put(`/ai-model-config/${id}`, updateData, false, true);
  },
  
  /**
   * 删除AI模型配置
   * @param {number|string} id - 配置ID
   * @returns {Promise} - HTTP DELETE请求的Promise对象
   */
  delete: (id: number | string) => {
    console.log(`${AI_MODEL_PREFIX} API 删除配置:`, id);
    return http.delete(`/ai-model-config/${id}`, {}, false, true);
  },
  
  /**
   * 测试AI模型配置连接
   * @param {AIModelConfig} data - 配置信息
   * @returns {Promise} - HTTP POST请求的Promise对象
   */
  testConnection: (data: AIModelConfig) => {
    console.log(`${AI_MODEL_PREFIX} API 测试连接`);
    return http.post('/ai-model-config/test-connection', data, false, true, true);
  },
  
  /**
   * 设置默认配置
   * @param {number|string} id - 配置ID
   * @returns {Promise} - HTTP PUT请求的Promise对象
   */
  setDefault: (id: number | string) => {
    console.log(`${AI_MODEL_PREFIX} API 设置默认配置:`, id);
    return http.put(`/ai-model-config/${id}/default`, {}, false, true);
  },
  
  /**
   * 修改配置启用状态
   * @param {number|string} id - 配置ID
   * @param {boolean} isEnabled - 是否启用
   * @returns {Promise} - HTTP PUT请求的Promise对象
   */
  changeStatus: (id: number | string, isEnabled: boolean) => {
    console.log(`${AI_MODEL_PREFIX} API 修改配置状态: ID=${id}, 启用=${isEnabled}`);
    return http.put(`/ai-model-config/${id}/status`, { isEnabled }, false, true);
  }
}; 